package com.example.a10928.weifriend.ui.home;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.example.a10928.weifriend.R;
import com.example.a10928.weifriend.databinding.FragmentHomeBinding;
import com.example.a10928.weifriend.http.api.Api;
import com.example.a10928.weifriend.http.api.VariableName;
import com.example.a10928.weifriend.ui.BaseFragment;
import com.example.a10928.weifriend.ui.MainActivity;
import com.example.a10928.weifriend.ui.home.menu.ActiveCenterActivity;
import com.example.a10928.weifriend.ui.home.menu.CarStoreActivity;
import com.example.a10928.weifriend.ui.home.menu.LotteryActivity;
import com.example.a10928.weifriend.ui.home.menu.NoticeActivity;
import com.example.a10928.weifriend.ui.home.menu.QRKActivity;
import com.example.a10928.weifriend.ui.home.menu.ServiceActivity;
import com.example.a10928.weifriend.ui.home.menu.UpdateVIPActivity;
import com.example.a10928.weifriend.ui.home.menu.WerbenActivity;
import com.example.a10928.weifriend.util.RxUtil;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.bgabanner.BGABanner;
import cn.bingoogolapple.bgabanner.BGALocalImageSize;

import static com.example.a10928.weifriend.App.app;

public class HomeFragment extends BaseFragment {

    private FragmentHomeBinding mBinding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false);
        mBinding.setView(this);
        initView();
        return mBinding.getRoot();
    }

    private void initView() {
        mBinding.homeBanner.setAdapter((banner, itemView, model, position) -> {
            Picasso.with(getContext()).load((String)model).into((ImageView) itemView);
            itemView.setBackgroundResource(R.drawable.banner);
        });

       // mBinding.homeLottery.getIv();
        RequestOptions options=new RequestOptions().diskCacheStrategy(DiskCacheStrategy.RESOURCE);
     //   Glide.with(getActivity()).load(R.mipmap.yiyuan).apply(options).into(mBinding.homeLottery.getIv());
     /*   BGALocalImageSize bgaLocalImageSize = new BGALocalImageSize(
                mBinding.homeBanner.getWidth(),
                mBinding.homeBanner.getHeight(),
                mBinding.homeBanner.getWidth(),
                mBinding.homeBanner.getHeight());
        mBinding.homeBanner.setData(bgaLocalImageSize,
                ImageView.ScaleType.FIT_XY
                , R.drawable.banner, R.drawable.banner, R.drawable.banner);*/
        mBinding.homeTvInform.setSelected(true);
        mBinding.homeNewVips.setSelected(true);
    }

    public void onViewClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.home_wisdom:
                intent.putExtra(WebViewActivity.DADA_WEBVIEW_URL, Api.H5_MAKE_MONEY);
                intent.putExtra(WebViewActivity.DADA_WEBVIEW_TITLE, WebViewActivity.SOURCE_MAKE_MONEY);
                intent.setClass(getContext(), WebViewActivity.class);
                break;
            case R.id.home_vip:
                intent.setClass(getContext(), UpdateVIPActivity.class);
                break;
            case R.id.home_qr:
                intent.setClass(getContext(), QRKActivity.class);
                break;
            case R.id.home_new:
                intent.putExtra(WebViewActivity.DADA_WEBVIEW_URL, Api.H5_NEW_GUIDE);
                intent.putExtra(WebViewActivity.DADA_WEBVIEW_TITLE, WebViewActivity.SOURCE_NEW_GUIDE);
                intent.setClass(getContext(), WebViewActivity.class);
                break;
            case R.id.home_werben:
                intent.setClass(getContext(), WerbenActivity.class);
                break;
            case R.id.home_service:
                intent.setClass(getContext(), ServiceActivity.class);
                break;
            case R.id.home_car:
                intent.setClass(getContext(), CarStoreActivity.class);
                intent.putExtra(VariableName.DATA,"http://www.reechauto.com/");
                break;
/*            case R.id.home_lottery:
                intent.putExtra("lotteryType", 1);
                intent.setClass(getContext(), ActiveCenterActivity.class);
                break;*/
            case R.id.home_shopping:
                intent.putExtra("lotteryType", 2);
                intent.setClass(getContext(), LotteryActivity.class);
                break;
            case R.id.home_tv_inform:
                intent.setClass(getContext(), NoticeActivity.class);
                break;
        }
        startActivity(intent);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getNewNotice(); getAllBanner();
    }

    @SuppressLint("CheckResult")
    public void getNewNotice() {
        ((MainActivity) getActivity()).showProgress();
        app().getBpService().getNewNotice()
                .compose(RxUtil.io2main())
                .subscribe(
                        r -> {
                            ((MainActivity) getActivity()).dismmProgress();
                            mBinding.homeTvInform.setText(r.getData().getNotice_content());
                        }, RxUtil::errorException
                );

        app().getBpService().getNewvipData(app().getUser().getUserId())
                .compose(RxUtil.io2main())
                .subscribe(
                        r -> {
                            String data = "";
                            for (int i = 0; i < r.getData().size(); i++) {
                                data = data + "恭喜 " + r.getData().get(i).getUser_name() + " 开通了VIP    ";
                            }
                            mBinding.homeNewVips.setText(data);
                        }, RxUtil::errorException
                );
    }

    @SuppressLint("CheckResult")
    public void getAllBanner() {
        ((MainActivity) getActivity()).showProgress();
        app().getBpService().getAllBanner("1")
                .compose(RxUtil.io2main())
                .subscribe(
                        r -> {
                            ((MainActivity) getActivity()).dismmProgress();
                            List<String> banner_imgurl = new ArrayList<>();
                            List<String> tips = new ArrayList<>();
                            for (int i = 0; i < r.getData().size(); i++) {
                                banner_imgurl.add(r.getData().get(i).getBanner_imgurl());
                                tips.add("");
                            }
                            mBinding.homeBanner.setData(banner_imgurl,tips);
                        }, RxUtil::errorException
                );
    }


}
